package com.tongqu.configbean;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class InitServer {

    @Bean(name = "workGroup")
    public NioEventLoopGroup getWorkGroup() {
        return new NioEventLoopGroup();
    }

    @Bean(name = "bossGroup")
    public NioEventLoopGroup getBossGroup() {
        return new NioEventLoopGroup();
    }

    @Bean(name = "serverBootstrap")
    public ServerBootstrap getServerBootstrap(@Qualifier("workGroup") NioEventLoopGroup boss, @Qualifier("bossGroup") NioEventLoopGroup work, @Autowired NettyChannelInitializer channelInitializer) {
        System.out.println("注入channelInitializer---Bean");

        return new ServerBootstrap().group(boss, work).channel(NioServerSocketChannel.class).childHandler(channelInitializer);
    }

    @Bean("format")
    public SimpleDateFormat getSimpleDateFormat() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format;
    }

    @Bean("dateFormat")
    public SimpleDateFormat getDateFormat() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format;
    }

    @Bean("redisTemplate")
    public RedisTemplate getRedisTemplate(@Autowired RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        return redisTemplate;
    }

}
